package com.lili.price;

import java.util.Arrays;

/**
 * @Auther: 李 力
 * @Date: 2024/8/26
 * @Description: 188.买卖股票的最佳时机IV
 * @version: 1.0
 */
public class LeetCode188 {

    public int maxProfit(int k, int[] prices) {
        int[] buy = new int[k];
        int[] sell = new int[k];
        Arrays.fill(buy, Integer.MIN_VALUE);
        for (int price : prices) {
            buy[0] = Math.max(buy[0], -price);
            sell[0] = Math.max(sell[0], buy[0] + price);
            for (int i = 1; i < k; i++) {
                buy[i] = Math.max(buy[i], sell[i - 1] - price);
                sell[i] = Math.max(sell[i], buy[i] + price);
            }
        }
        return sell[k - 1];
    }
}
